<?php

namespace app\health\validate;

use think\Cache;

class SmsValidate extends BaseValidate
{
    protected $rule =   [
        'mobile'  => 'require|mobile|sendLimit',
    ];

    protected $message  =  [
        'mobile.require' => '请输入手机号',
        'mobile.mobile'  => '请输入正确的手机号',
    ];


    public function sendLimit($value, $rule, $data)
    {
        $mobile = $data['mobile'];
        if (Cache::get('sendSms:' . $data['scene'] . ':' . $mobile) >= 10) {
            return '请求过于频繁,请稍后再试';
        }
        if (Cache::has('sendSms:' . $data['scene'] . ':' . $mobile)) {
            Cache::inc('sendSms:' . $data['scene'] . ':' . $mobile, 1);
        } else {
            Cache::set('sendSms:' . $data['scene'] . ':' . $mobile, 1, 60);
        }
        return true;
    }
}
